package com.example.xuanmei.video.service;

import com.example.xuanmei.common.dto.ApiResponse;
import com.example.xuanmei.video.dto.VideoDto;
import com.example.xuanmei.video.dto.VideoUploadRequest;
import com.example.xuanmei.video.entity.Video;
import com.example.xuanmei.video.repository.VideoRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 视频服务
 */
@Service
@RequiredArgsConstructor
public class VideoService {
    private final VideoRepository videoRepository;
    
    /**
     * 上传视频
     */
    public ApiResponse<VideoDto> uploadVideo(Long userId, VideoUploadRequest request) {
        Video video = new Video();
        video.setUserId(userId);
        video.setTitle(request.getTitle());
        video.setCover(request.getCover());
        video.setVideoUrl(request.getVideoUrl());
        video.setDescription(request.getDescription());
        video.setCategory(request.getCategory());
        video.setDuration(request.getDuration());
        video.setLikes(0L);
        video.setComments(0L);
        video.setViews(0L);
        video.setShares(0L);
        
        Video savedVideo = videoRepository.save(video);
        return ApiResponse.success(convertToDto(savedVideo));
    }
    
    /**
     * 获取视频列表（推荐）
     */
    public ApiResponse<List<VideoDto>> getVideoList(int page, int size) {
        Pageable pageable = PageRequest.of(page, size);
        Page<Video> videoPage = videoRepository.findAllByOrderByCreateTimeDesc(pageable);
        
        List<VideoDto> videoDtos = videoPage.getContent().stream()
                .map(this::convertToDto)
                .collect(Collectors.toList());
        
        return ApiResponse.success(videoDtos);
    }
    
    /**
     * 根据用户ID获取视频列表
     */
    public ApiResponse<List<VideoDto>> getUserVideos(Long userId, int page, int size) {
        Pageable pageable = PageRequest.of(page, size);
        Page<Video> videoPage = videoRepository.findByUserId(userId, pageable);
        
        List<VideoDto> videoDtos = videoPage.getContent().stream()
                .map(this::convertToDto)
                .collect(Collectors.toList());
        
        return ApiResponse.success(videoDtos);
    }
    
    /**
     * 获取视频详情
     */
    public ApiResponse<VideoDto> getVideoDetail(Long videoId) {
        Video video = videoRepository.findById(videoId)
                .orElseThrow(() -> new RuntimeException("视频不存在"));
        
        // 增加播放量
        video.setViews(video.getViews() + 1);
        videoRepository.save(video);
        
        return ApiResponse.success(convertToDto(video));
    }
    
    /**
     * 点赞视频
     */
    public ApiResponse<Void> likeVideo(Long videoId) {
        Video video = videoRepository.findById(videoId)
                .orElseThrow(() -> new RuntimeException("视频不存在"));
        video.setLikes(video.getLikes() + 1);
        videoRepository.save(video);
        return ApiResponse.success(null);
    }
    
    private VideoDto convertToDto(Video video) {
        VideoDto dto = new VideoDto();
        dto.setId(video.getId());
        dto.setUserId(video.getUserId());
        dto.setTitle(video.getTitle());
        dto.setCover(video.getCover());
        dto.setVideoUrl(video.getVideoUrl());
        dto.setDescription(video.getDescription());
        dto.setLikes(video.getLikes());
        dto.setComments(video.getComments());
        dto.setViews(video.getViews());
        dto.setShares(video.getShares());
        dto.setDuration(video.getDuration());
        dto.setCategory(video.getCategory());
        dto.setCreateTime(video.getCreateTime());
        
        // 模拟作者信息（实际应该从用户服务获取）
        dto.setAuthorName("用户" + video.getUserId());
        dto.setAuthorAvatar("https://example.com/avatar.jpg");
        
        return dto;
    }
}
